package com.servlet.dept;

import com.bean.Department;
import com.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/editdept")
public class EditDeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        AdminService adminService=new AdminService();
        String dnumber=req.getParameter("dnumber");
        String dname=req.getParameter("dname");
        if (dnumber.length() != 2||!adminService.judgeContainsStr(dnumber)){
            req.setAttribute("Message", "部门号不满足要求，修改失败！");
            req.getRequestDispatcher("adminadddept.jsp").forward(req, resp);
            return;
        }
        if (!adminService.getDept(dnumber)){
            req.setAttribute("Message", "部门不存在，修改失败！");
            req.getRequestDispatcher("admingetfail.jsp").forward(req, resp);
            return;
        }
        Department department=new Department(dnumber,dname);
        adminService.updateDept(department);
        req.setAttribute("dept",department);
        req.getRequestDispatcher("admingetdept.jsp").forward(req, resp);
    }
}
